using IM.Easy.Infrastructure.Query;
using Microsoft.AspNetCore.Mvc;
using IM.Easy.SystemServices.IServices;
using IM.Easy.Entity.System;
using IM.Easy.CoreServices.Dto;
using IM.Easy.CoreServices.IServices;
using IM.Easy.Api.Filters;
using IM.Easy.Core.Attributes;

namespace IM.Easy.Api.Controllers.System
{
    /// <summary>
    /// AuthConfigController 控制器
    /// </summary>
    [Route("system/authconfig")]
    public class AuthConfigController : ControllerBase
    {
        private readonly IAuthConfigService _service;

        /// <summary>
        /// 构造函数，注入服务
        /// </summary>
        /// <param name="service">服务接口</param>
        public AuthConfigController(IAuthConfigService service)
        {
            _service = service;
        }

        /// <summary>
        /// 获取所有SysAuthConfig对象
        /// </summary>
        /// <returns>返回SysAuthConfig列表</returns>
        [HttpGet]
        [Api(Name = "获取授权列表", Perm = "auth:config:list")]
        public PageResponse<SysAuthConfig> GetList([FromQuery] SysAuthConfigQueryDto query)
        {
            return _service.GetPageList(query);
        }

        /// <summary>
        /// 根据ID获取SysAuthConfig对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <returns>返回指定的SysAuthConfig对象</returns>
        [HttpGet("{id}")]
        [Api(Name = "获取授权配置", Perm = "auth:config:info")]
        public SysAuthConfig GetById([FromRoute] long id)
        {
            var result = _service.GetById(id);
            return result ?? throw new KeyNotFoundException($"未找到ID为 {id} 的SysAuthConfig对象。");
        }

        /// <summary>
        /// 创建新的SysAuthConfig对象
        /// </summary>
        /// <param name="entity">要创建的SysAuthConfig对象</param>
        /// <returns>返回创建后的SysAuthConfig对象</returns>
        [HttpPost]
        [Api(Name = "添加授权配置", Perm = "auth:config:add")]
        public SysAuthConfig Create([FromBody] SysAuthConfig entity)
        {
            _service.Create(entity);
            return entity; // 直接返回创建的实体
        }

        /// <summary>
        /// 更新现有的SysAuthConfig对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <param name="entity">更新后的SysAuthConfig对象</param>
        [HttpPut]
        [SuperAdmin]
        [Api(Name = "更新授权配置", Perm = "auth:config:put")]
        public void Update([FromBody] SysAuthConfig entity)
        {
            _service.Update(entity);
        }

        /// <summary>
        /// 批量删除SysAuthConfig对象
        /// </summary>
        /// <param name="ids">对象的IDS</param>
        [HttpDelete]
        [SuperAdmin]
        [Api(Name = "批量删除授权配置", Perm = "auth:config:bulk:del")]
        public void Delete([FromBody] long[] ids)
        {
            _service.Delete(ids);
        }

        /// <summary>
        /// 单个删除SysAuthConfig对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        [HttpDelete("{id}")]
        [SuperAdmin]
        [Api(Name = "删除授权配置", Perm = "auth:config:del")]
        public void Delete([FromRoute] long id)
        {
            _service.Delete(id);
        }
    }
}
